//
//  JPHttpManagerClass.m
//  JPShop
//
//  Created by 虎爷 on 2019/9/27.
//  Copyright © 2019 虎爷. All rights reserved.
//

#import "CDNetworkRequset.h"

@implementation CDNetworkRequset

-(instancetype)init{
    return [CDNetworkRequset manager];
}

+(instancetype)HttpRequstManager{
    CDNetworkRequset *req = [CDNetworkRequset manager];
    req.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",nil];
    
    req.requestSerializer = [AFJSONRequestSerializer serializer];
    req.responseSerializer = [AFJSONResponseSerializer serializer];
    req.requestSerializer.timeoutInterval = 10.0f;
    req.securityPolicy = [AFSecurityPolicy defaultPolicy];
    req.securityPolicy.allowInvalidCertificates = YES;
    req.securityPolicy.validatesDomainName = NO;
    [req.requestSerializer setValue:@"123" forHTTPHeaderField:@"tt"];
    return req;
}

-(void)PostURL:(NSString *)appendURl
        params:(NSDictionary *)params
       success:(successBlock)sucess
          fail:(failBlock)fail{
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
   
    NSMutableDictionary *bd = [[NSMutableDictionary alloc]init];
    bd[@"productId"] = @"401";
    bd[@"productName"] = @"去图标";
    bd[@"version"] = @"1001";
    bd[@"channelId"] = @"10201";
    bd[@"hardwareCode"] = @"imei861942040024664";
    bd[@"os"] = @"100";
    bd[@"customKey"] = @"8814416270";
    bd[@"appId"] = @"";
    
    dic[@"b"] = bd;
    
    NSMutableDictionary *od;
    if ([params isKindOfClass:[NSNull class]] || [params isEqual:[NSNull null]]) {
        od = [[NSMutableDictionary alloc]init];
    }else{
        od = [NSMutableDictionary dictionaryWithDictionary:params];
    }
    dic[@"o"] = od;
    [self POST:appendURl parameters:dic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%@",appendURl);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger statusNum = [responseObject[@"code"] integerValue];

        if (sucess) {
            if (statusNum == 1) {
                sucess(statusNum, responseObject);
         
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络错误地址=%@",appendURl);
        if (fail) {
            fail(error);
        }
    }];
}


@end
